package com.glorysoft.wangnianxinpda.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityEngineerModelBinding;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

public class EngineerModelActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private ActivityEngineerModelBinding binding;
    private String sEqpId = "";
    private String sIsEqp = "";
    private String sIsLot = "";
    private String sIsMaterial = "";
    private String sIsSap = "";
    private String sWorkNo = "";
    private String sPwd = "";
    private String sLot = "";

    @Override
    protected int getLayoutResId() {
        return super.getLayoutResId();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_engineer_model);
        binding.rgEqp.setOnCheckedChangeListener(this);
        binding.rgLot.setOnCheckedChangeListener(this);
        binding.rgMaterial.setOnCheckedChangeListener(this);
        binding.rgSap.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        sEqpId = extras.getString("EQPID");
        binding.setSEqpId(sEqpId);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.engineerModel));
        freeUI_titleView.setRightListener(getString(R.string.Submit), v -> submit());
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), binding.llMain);
    }

    private void submit() {
        sWorkNo = binding.etWorkNo.getText().toString();
        sPwd = binding.etPwd.getText().toString();
        sLot = binding.etLotId.getText().toString();
        if (sWorkNo.equals("")){
            showToast(getString(R.string.please_input_unlock_user_work_no), false);
            return;
        }
        if (sPwd.equals("")){
            showToast(getString(R.string.please_input_unlock_user_pwd), false);
            return;
        }
        if (sLot.equals("")){
            showToast(getString(R.string.please_input_lot), false);
            return;
        }
        cleanLot();

        //region 测试
//        showMessageDialog("信息清除成功", getSupportFragmentManager());
        //endregion
    }

    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");

    }

    //region 接口调用

    private void cleanLot(){
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
//                    finish();
                    showMessageDialog(response.body().getMsg(), getSupportFragmentManager());

                }
            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDATrackIn");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", sWorkNo);//工号
        map.addProperty("EQPID", sEqpId);//设备号
        map.addProperty("LotID", sLot);//批次
        map.addProperty("ISEQP", sIsEqp);//是否清理设备数据
        map.addProperty("ISLOT", sIsLot);//是否清理批次数据
        map.addProperty("ISMAT", sIsMaterial);//是否清理物料数据
        map.addProperty("ISSAP", sIsSap);//是否做SAP退步
        map.addProperty("PASSWORD", sPwd);//密码（MD5加密后的）
        map.addProperty("LOTID", sPwd);//批次号

        String method = "PDACleanLot";
        NetUtils.requestPostNet("PDACleanLotRequest", method, this, map, callBack);
    }
    //endregion

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group == binding.rgEqp){
            sIsEqp = checkedId == R.id.rb_eqp_ok ? "OK" : "NG";
        }else if (group == binding.rgLot){
            sIsLot = checkedId == R.id.rb_lot_ok ? "OK" : "NG";
        }else if (group == binding.rgMaterial){
            sIsMaterial = checkedId == R.id.rb_material_ok ? "OK" : "NG";
        }else {
            sIsSap = checkedId == R.id.rb_sap_ok ? "OK" : "NG";
        }
    }
}